<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters">
                <el-form-item>
                    <el-input v-model="filters.name" placeholder="名称"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="getRoles">查询</el-button>
                </el-form-item>
                <el-form-item style="float:right;" >
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>

        <el-table :data="list" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="60">
            </el-table-column>
            <el-table-column prop="roleName" label="角色名称"  sortable>
            </el-table-column>
            <el-table-column prop="description" label="描述" width="480" >
            </el-table-column>
            <el-table-column prop="status" label="状态" width="180" sortable :formatter="formatStatus"  >
            </el-table-column>
            <el-table-column label="操作" width="320">
                <template slot-scope="scope">
                    <el-button type="success" size="small" @click="handlePermission(scope.$index, scope.row)">权限设置</el-button>
                    <el-button type="primary" size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--新增界面-->
        <el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
            <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
                <el-form-item label="名称" prop="name">
                    <el-input v-model="addForm.roleName" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="描述" prop="description" >
                    <el-input type="tel" v-model="addForm.description"  auto-complete="off" ></el-input>
                </el-form-item>
                <el-form-item  label="状态" prop="status" >
                    <el-radio-group v-model="addForm.status" >
                        <el-radio class="radio" :label="1"  >启用</el-radio>
                        <el-radio class="radio" :label="0"  >禁用</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>

        <!--更新界面-->
        <el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
            <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
                <el-form-item label="名称" prop="name">
                    <el-input v-model="editForm.roleName" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="描述" prop="description" >
                    <el-input type="tel" v-model="editForm.description"  auto-complete="off" ></el-input>
                </el-form-item>
                <el-form-item  label="状态" prop="status" >
                    <el-radio-group v-model="editForm.status" >
                        <el-radio class="radio" :label="1"  >启用</el-radio>
                        <el-radio class="radio" :label="0"  >禁用</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="editFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
            </div>
        </el-dialog>

        <el-dialog title="设置权限" :visible.sync="permissionFormVisible" :close-on-click-modal="false" >
            <el-tree
                    :data="menus"
                    ref="tree"
                    show-checkbox
                    node-key="id"
                    :default-expanded-keys="[2, 3]"
                    :default-checked-keys="defaultCheckedKeys"
                    :props="defaultProps">
            </el-tree>

            <div slot="footer" class="dialog-footer">
                <el-button @click.native="permissionFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="permsSubmit" :loading="permsLoading">提交</el-button>
            </div>
        </el-dialog>

    </section>
</template>

<script>
    import {getRoleList, saveRole, deleteRoleApi, getMenuTree, saveRoleMenus} from '../../api/api'
    export default {
        data() {
            return {
                filters: {
                    name: ''
                },
                list: [],
                listLoading: false,
                sels: [],
                addFormVisible: false,
                editFormVisible: false,
                permissionFormVisible: false,
                addLoading: false,
                editLoading: false,
                permsLoading: false,
                addForm :{
                    roleName: '',
                    status: 1,
                    description: ''
                },
                editForm :{
                    roleName: '',
                    status: 1,
                    description: ''
                },
                addFormRules: {
                    roleName: [
                        { required: true, message: '请输入角色名称', trigger: 'blur' }
                    ]
                },
                editFormRules: {
                    roleName: [
                        {required: true, message: '请输入角色名称', trigger: 'blur'}
                    ]
                },
                menus: [],
                defaultProps: {
                    children: 'children',
                    label: 'name'
                },
                defaultCheckedKeys: [],
                currentRoleId:0
            }
        },
        methods: {
            getRoles: function () {
                let para = {
                    name: this.filters.name
                };
                this.listLoading = true;
                //NProgress.start();
                getRoleList(para).then((res) => {
                    let data = res.data.data;
                    this.list = data;
                    this.listLoading = false;
                    //NProgress.done();
                });
            },
            handleAdd: function (index,row) {
                this.addFormVisible = true;
                this.addForm = {
                    roleName: '',
                    status: 1,
                    description: ''
                }
                if(row){
                    this.addForm.parentId = row.id
                }
            },
            handleEdit: function(index,row){
                this.editFormVisible = true;
                this.editForm = {
                    id:row.id,
                    roleName: row.roleName,
                    status:row.status,
                    description:row.description
                }
            },
            selsChange: function (sels) {
                this.sels = sels;
            },
            //新增
            addSubmit: function () {
                let _this = this;
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.addLoading = true;
                        //NProgress.start();
                        let para = Object.assign({}, this.addForm);
                        // para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
                        saveRole(para).then((res) => {
                            if(res.code == 200){
                                this.addLoading = false;
                                //NProgress.done();
                                this.$message({
                                    message: '提交成功',
                                    type: 'success'
                                });
                                this.$refs['addForm'].resetFields();
                                this.addFormVisible = false;
                                this.getRoles();
                            }else{
                                _this.$message({
                                    type: 'error',
                                    message: res.msg
                                })
                            }
                        });
                    }
                });
            },
            editSubmit:function(){
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.editLoading = true;
                        //NProgress.start();
                        let para = Object.assign({}, this.editForm);
                        // para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
                        saveRole(para).then((res) => {
                            if(res.code == 200){
                                this.editLoading = false;
                                //NProgress.done();
                                this.$message({
                                    message: '编辑成功',
                                    type: 'success'
                                });
                                this.$refs['editForm'].resetFields();
                                this.editFormVisible = false;
                                this.getRoles();
                            }
                        });
                    }
                });
            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    let para = { id: row.id };
                    console.log(para)
                    deleteRoleApi(para).then((res) => {
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getRoles();
                    });
                }).catch((e) => {
                    this.listLoading = false;
                    this.$message({
                        message: e,
                        type: 'error'
                    });
                });
            },
            formatStatus:function (row,column) {
                return   row.status == 1 ? '正常' : row.status == 0 ? '已禁用' : '未知';
            },
            handlePermission:function (index,row) {
                this.permissionFormVisible = true
                let menus = row.roleMenus
                this.defaultCheckedKeys = []
                this.currentRoleId = row.id
                menus.forEach((item,index)=>{
                    this.defaultCheckedKeys.push(item.menuId)
                })
            },
            getCheckedKeys:function() {
                return this.$refs.tree.getCheckedKeys();
            },
            permsSubmit: function () {
                this.$confirm('确认提交吗？', '提示', {}).then(() => {
                    this.permsLoading = true;
                    let _this = this;
                    let para = Object.assign({},{
                       keys : this.getCheckedKeys(),
                       roleId: _this.currentRoleId
                    });
                    saveRoleMenus(para).then((res) => {
                        if(res.code == 200){
                            this.permsLoading = false;
                            //NProgress.done();
                            this.$message({
                                message: '设置成功',
                                type: 'success'
                            });
                            this.permissionFormVisible = false;
                            this.getRoles();
                        }
                    });
                });
            },
            getMenus:function () {
                let _this = this;
                getMenuTree().then((res)=>{
                    let data = res.data.data;
                    _this.menus = data;
                    console.log(data)
                })
            }
        },
        mounted() {
            this.getRoles()
            this.getMenus()
        }
    }
</script>

<style scoped>

</style>
